﻿using Meow.Core;
using Silk.NET.Input;
using Silk.NET.OpenGL;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Test
{
    internal class Learn_03_Abstractions : LearnClass
    {
        string LearnClass.Name => "03: Abstractions";
        Rd_Context rdContext;
        Rd_VertexPrototype vertexPrototype;
        Rdb_TestRenderable meshRenderable;
        float[] vertices =
        {
            //      aPosition       | aTexCoords
                 0.5f,  0.5f, 0.0f,  1.0f, 1.0f,
                 0.5f, -0.5f, 0.0f,  1.0f, 0.0f,
                -0.5f, -0.5f, 0.0f,  0.0f, 0.0f,
                -0.5f,  0.5f, 0.0f,  0.0f, 1.0f
        };
        uint[] indices =
        {
                0u, 1u, 3u,
                1u, 2u, 3u
        };

        void LearnClass.OnLoad()
        {
            if (Program._Window == null)
            {
                throw new Exception("Need call after _Window init");
            }

            rdContext = new Rd_Context(Program._Window);
            rdContext.ClearColor(Color.CornflowerBlue);

            Rd_VertexPrototyeDef vertDef = new Rd_VertexPrototyeDef();
            vertDef.Types = new VertexAttribPointerType[2]
            {
                VertexAttribPointerType.Float, // Pos
                VertexAttribPointerType.Float, // UV
            };
            vertDef.Segments = new int[2]
            {
                3, // Pos
                2, // UV
            };

            vertexPrototype = new Rd_VertexPrototype(vertDef);
            rdContext.RegisterVertexPrototype(vertexPrototype);

            // 绑定vertexPrototype， 类似于绑定VAO
            rdContext.CurrentVertexPrototype = vertexPrototype;

            // 生成mesh，它只负责将数据存储进一个buffer中，并且必须在绑定vertexPrototype后调用
            // （正常只是生成buffer的话其实也不一定要绑定到哪个VAO上，有一个就行，
            //  但是这里还会将Rd_Mesh的vertexPrototype设定为当前 rdContext.CurrentVertexPrototype，以防止混乱引用和渲染mesh）
            var mesh = rdContext.CreateMesh(vertices, indices, MeshType.Common);

            rdContext.UnbindAllBuffer(); // 防止出错

            // 生成Shader
            var shaderDef = new Rd_ShaderDef()
            {
                Name = "03_Abstractions",
                VertexCodePath = Path.Combine(MeowPath.DefaultShaderPath, "02_Vert.glsl"),
                FragmentCodePath = Path.Combine(MeowPath.DefaultShaderPath, "02_Frag.glsl"),
            };
            var shader = new Rd_Shader(shaderDef, rdContext);

            // 读取贴图
            rdContext.LoadTexture(Path.Combine(Def.TexturePath, "container.png"), out var _texture);
            rdContext.LoadTexture(Path.Combine(Def.TexturePath, "awesomeface.png"), out var _texture2);
            
            // 生成一个Rdb —— Renderable ，指的是可渲染的对象
            meshRenderable = new Rdb_TestRenderable()
            {
                VertexPrototype = vertexPrototype,
                Shader = shader,
                Texture1 = _texture,
                Texture2 = _texture2,
            };
            meshRenderable.AddMesh(mesh);

        }

        void LearnClass.OnRender(double deltaTime)
        {
            rdContext.Clear(ClearBufferMask.ColorBufferBit);

            meshRenderable.Render(rdContext);
        }

        void LearnClass.OnKeyDown(IKeyboard keyboard, Key key, int keyCode) { }

        void LearnClass.OnUnload(){}

        void LearnClass.OnUpdate(double deltaTime) { }


    }
}
